package com.example.switchdemo.slice;

import com.example.switchdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Switch;
import ohos.agp.components.element.ShapeElement;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取开关组件实例对象
        Switch swi = (Switch)findComponentById(ResourceTable.Id_switch_component);
        // 设置开启状态的文本
        swi.setStateOnText("开启");
        // 设置关闭状态的文本
        swi.setStateOffText("关闭");
        // 设置背景
        ShapeElement track = new ShapeElement();
        track.setShape(ShapeElement.RECTANGLE);
        track.setRgbColor(new RgbColor(255, 0, 0));
        track.setCornerRadius(60);
        swi.setTrackElement(track);
        // 设置开关按钮背景
        ShapeElement thumb = new ShapeElement();
        thumb.setShape(ShapeElement.RECTANGLE);
        thumb.setCornerRadius(60);
        thumb.setRgbColor(new RgbColor(0, 255, 0));
        swi.setThumbElement(thumb);
        // 设置开关状态
        swi.setChecked(true);
        // 添加开关状态变化的监听
        swi.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                // 获取开关状态
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
